package com.cw.mybatis.starter.wrapper;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.mybatis.starter.model.BaseDo;
import com.cw.starter.center.dto.BaseDto;

import cn.hutool.core.util.StrUtil;

/**
 * 构建BaseDto
 *
 * @author chenw
 * @date 2021/11/10
 */
public class BaseWrapper<T extends BaseDo> {
    public void buildBaseWrapper(LambdaQueryWrapper<T> lambdaQueryWrapper, BaseDto baseDto) {
        if (null != baseDto.getId()) {
            lambdaQueryWrapper.eq(T::getId, baseDto.getId());
        }
        if (StrUtil.isNotBlank(baseDto.getIds())) {
            String[] ids = baseDto.getIds()
                .split(",");
            List<String> idList = Arrays.asList(ids);
            List<Long> longIds = idList.stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());
            lambdaQueryWrapper.in(T::getId, longIds);
        }
        if (null != baseDto.getCreateBy()) {
            lambdaQueryWrapper.eq(T::getCreateBy, baseDto.getCreateBy());
        }
        if (null != baseDto.getCreateTimeStart()) {
            lambdaQueryWrapper.ge(T::getCreateTime, baseDto.getCreateTimeStart());
        }
        if (null != baseDto.getCreateTimeEnd()) {
            lambdaQueryWrapper.le(T::getCreateTime, baseDto.getCreateTimeEnd());
        }
        if (null != baseDto.getUpdateBy()) {
            lambdaQueryWrapper.eq(T::getUpdateBy, baseDto.getUpdateBy());
        }
        if (null != baseDto.getUpdateTimeStart() && null != baseDto.getUpdateTimeEnd()) {
            lambdaQueryWrapper.ge(T::getUpdateTime, baseDto.getUpdateTimeStart());
        }
        if (null != baseDto.getUpdateTimeEnd() && null != baseDto.getUpdateTimeEnd()) {
            lambdaQueryWrapper.le(T::getUpdateTime, baseDto.getUpdateTimeEnd());
        }
        lambdaQueryWrapper.orderByDesc(T::getCreateTime);

    }
}
